<?php
/**
 * Created by PhpStorm.
 * User: yaolei
 * Date: 2017/11/16
 * Time: 23:35
 */

namespace Home\Model;
use Think\Model;

class Bill extends Model
{
    protected $tableName = 'user_balance_bill';
    protected $_validate = [
        ['user_id','require','请选择用户'],
        ['to_user_id','require','请选择被转入用户'],
        ['number','require','请输入转入数量'],
        ['number','integer','转入数请输入整数'],
        ['description','require','请填写说明']
    ];

    public function getData(){
        $data=$this->data;
        if(!empty($data['user_id'])){
            $users= new Model('user_balance');
            $data['user']=$users->where('user_id='.$data['user_id'])->find();
        }
        if(!empty($data['to_user_id'])){
            $users= new Model('user_balance');
            $data['touser']=$users->where('user_id='.$data['to_user_id'])->find();
        }
        return $data;
    }

    public function generateSN(){
        $sn="";
        $Model = new Model('user_balance_bill');
        do {
            $sn= date('Ymd') . str_pad(mt_rand(1, 9999999999),10, '0', STR_PAD_LEFT);
            $count=$Model->where("bill_sn='".$sn."'")->count();
        } while ($count>0);
        $this->bill_sn=$sn;
    }

    public function add(){
        $data=$this->getData();
        $user=$data['user'];
        $to=$data['touser'];

        if(empty($user)){
            $this->error="用户信息有误，请重新选择";
        }
        if(empty($to)){
            $this->error="转入用户有误，请重新选择";
        }
        $number=floor($data['number']);

        if($number<=0){
            $this->error="转入数必须大于0";
            return false;
        }
        if($number>$user['balance']){
            $this->error="扣除的余额不能大于".$user['balance'];
            return false;
        }

        $model = new Model();
        $model->startTrans();
        $flag=false;
        $user_balance=$user['balance']-$this->number;
        $touser_balance=$to['balance']+$this->number;

        $res1=$model->table(C('DB_PREFIX').'user_balance')->where("id=".$user['id'])->save([
            'balance'=>$user_balance,
            'use_balance'=>$user['use_balance']+$this->number,
            'updatedtime'=>time()
        ]);
        $res2=$model->table(C('DB_PREFIX').'user_balance')->where("id=".$to['id'])->save([
            'balance'=>$touser_balance,
            'total_balance'=>$to['total_balance']+$this->number,
            'updatedtime'=>time()
        ]);
        if($res1 && $res2){
                //用户支出余额
                $model->table(C('DB_PREFIX').'user_balance_log')->add([
                    'user_id'=>$user['user_id'],
                    'type'=>1,
                    'current_balance'=>$user['balance'],
                    'balance'=>$this->number,
                    'total_balance'=>$user_balance,
                    'createdtime'=>time(),
                    'description'=>'向用户：'.$to['username'].'转入余额：'.$this->number
                ]);
                //转入用户收入余额
                $model->table(C('DB_PREFIX').'user_balance_log')->add([
                    'user_id'=>$to['user_id'],
                    'type'=>0,
                    'current_balance'=>$to['balance'],
                    'balance'=>$this->number,
                    'total_balance'=>$touser_balance,
                    'createdtime'=>time(),
                    'description'=>'用户：'.$user['username'].'给您转入余额：'.$this->number
                ]);
            $flag=true;
        }
        if(!$flag){
            $model->rollback();
        }else{
            $this->createdtime=time();
            parent::add();
            $model->commit();
        }
        return $flag;
    }
}